package com.xspace.android.xafcommon.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public abstract class BaseDBHelper extends SQLiteOpenHelper {
	public SQLiteDatabase mDatabase = null;

	public BaseDBHelper(Context context, String DB_NAME, int DB_VERSION) {
		super(context, DB_NAME, null, DB_VERSION);
	}

	public void open() {
		try {
			if (mDatabase == null) {
				mDatabase = getWritableDatabase();
			}
			//			mDatabase.beginTransaction(); 
		} catch (Exception ex) {
			ex.printStackTrace();
			if(mDatabase!=null){
				mDatabase.close();
			}
			mDatabase = getWritableDatabase();
		}
	}

	public void close() {
//		if (null != mDatabase) {
//			//			mDatabase.endTransaction();
//			mDatabase.close();
//			mDatabase = null;
//		}
	}

	public synchronized int insertOrUpdate(String tableName, ContentValues values, String where) {
		open();
		int _ID = -1;
		try {
			Cursor cursor = mDatabase.query(tableName, null, where, null, null, null, null);
			if (cursor != null && cursor.moveToFirst()) {
				_ID = (int) mDatabase.update(tableName, values, where, null);
			} else {
				_ID = (int) mDatabase.insert(tableName, null, values);
			}
			if (cursor != null) {
				cursor.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			_ID = -1;
		} finally {
//			close();
		}
		return _ID;
	}

	public synchronized int insert(String tableName, ContentValues values) {
		open();
		int _ID = -1;
		try {
			_ID = (int) mDatabase.insert(tableName, null, values);
		} catch (Exception ex) {
			ex.printStackTrace();
			_ID = -1;
		} finally {
			close();
		}
		return _ID;
	}

	public synchronized int update(String tableName, ContentValues values, String where) {
		open();
		int _ID = -1;
		try {
			_ID = (int) mDatabase.update(tableName, values, where, null);
		} catch (Exception ex) {
			ex.printStackTrace();
			_ID = -1;
		} finally {
			close();
		}
		return _ID;
	}

	public synchronized int replace(String tableName, ContentValues values, String where) {
		open();
		int _ID = -1;
		try {
			_ID = (int) mDatabase.update(tableName, values, where, null);
		} catch (Exception ex) {
			ex.printStackTrace();
			_ID = -1;
		} finally {
			close();
		}
		return _ID;
	}

	public synchronized Cursor query(String tableName, String[] columns, String where) {
		Cursor cur = null;
		try {
			open();
			cur = mDatabase.query(tableName, columns, where, null, null, null, null);

		} catch (SQLException ex) {
			ex.printStackTrace();
			cur = null;
		}
		return cur;
	}

	public synchronized Cursor query(String tableName, String where) {
		Cursor cur = null;
		try {
			open();
			cur = mDatabase.query(tableName, null, where, null, null, null, null);

		} catch (SQLException ex) {
			ex.printStackTrace();
			cur = null;
		}
		return cur;
	}

	public synchronized Cursor query(String tableName, String[] columns, String where, String[] selectionArgs,
			String groupBy, String having, String orderBy) {
		Cursor cur = null;
		try {
			open();
			cur = mDatabase.query(tableName, columns, where, selectionArgs, groupBy, having, orderBy);

		} catch (SQLException ex) {
			ex.printStackTrace();
			cur = null;
		}
		return cur;
	}

	public synchronized int delete(String tableName, String selection, String[] selectionArgs) {
		int result = -1;
		try {
			open();
			result = mDatabase.delete(tableName, selection, selectionArgs);
		} catch (Exception ex) {
			ex.printStackTrace();
			result = -1;
		} finally {
			close();
		}
		return result;
	}

	public synchronized int queryMaxId(String table) {
		int maxId = -1;
		try {
			open();
			Cursor cur;
			String strSql = "select max(_ID) AS _ID from " + table;
			cur = mDatabase.rawQuery(strSql, null);
			if (null != cur && cur.getCount() != 0) {
				cur.moveToFirst();
				maxId = cur.getInt(cur.getColumnIndex("_ID"));
			}
			if (null != cur) {
				cur.close();
				cur = null;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			maxId = -1;
		} finally {
			close();
		}
		return maxId;
	}

}
